import storage from "./storage";
/**
 * base64 处理
 * @param str 
 */
 export function base64(str: any) {
    if(!isEmpty(str)){
        return  new Buffer(str).toString('base64')
    }else{
        return "";
    }
    
}
/**
 * base64 解码
 * @param str 
 */
export function decodeBase64(str: any) {
    if(!isEmpty(str)){
        return  new Buffer(str,"base64").toString();
    }else{
        return "";
    }
    
}
/**
 * 为空判断
 * @param obj 
 */
 export function isEmpty(obj: any){
    if(typeof obj === "undefined" || obj === null || obj === "" || JSON.stringify(obj) == "{}"){
        return true;
    }else{
        return false;
    }
}
/**
 * 字符串是否为空
 * @param str 
 * @returns 
 */
export function isStringEmpty(str?:string){
    return str?.trim() === '';
}
/**
 * 深度复制
 * @param obj 对象
 * @returns 新对象
 */
export function deepCopy(obj:any){
    if(!isEmpty(obj)){
        return JSON.parse(JSON.stringify(obj));
    }
    return obj;
}

/**
 * 按钮权限
 * @param locationKey 权限key
 * @param searchBtn 查询按钮
 * @param btnList 权限列表
 * @returns 
 */
 export function buttonPermissions(locationKey: string, searchBtn:string,btnList: Array<any>) {
    // 按钮权限
    if (Array.isArray(btnList) && btnList.indexOf(locationKey + "-" + searchBtn + "-") != -1) {
      return true;
    }
    return false;
  }
  /**
   * 页面权限
   * @param searchPage 查询页面
   * @param pageList 权限列表
   * @returns 
   */
   export function pagePermissions(searchPage: string,pageList: Array<any>) {
      if (Array.isArray(pageList) && pageList.indexOf(searchPage) != -1) {
          return true;
      }
      return false;
  }
  /**
   * 获取权限列表
   * @param default_button_auth 权限
   * @param extraAuth 额外权限
   * @returns 
   */
   export function getPermissionsList(default_button_auth: any,extraAuth: any) {
      let pageAuth= [];
      let btnAuth = [];
      for (let index1 = 0; index1 < default_button_auth.length; index1++) {
          const element1 = default_button_auth[index1];
          const extraElement1 = extraAuth[index1];
          if (element1.children && Array.isArray(element1.children)) {
              for (let index2 = 0; index2 < element1.children.length; index2++) {
                  const element2 = element1.children[index2];
                  const extraElement2 =extraElement1? extraElement1.children[index2] : null;
                  if (element2.btn) {
                      let element2List = [];
                      for (const [key, val] of Object.entries(element2.btn)) {
                          element2List.push(val);
                      }
                      let extraElement2List = [];
                      if (extraElement2) {
                          for (const [key, val] of Object.entries(extraElement2.btn)) {
                              extraElement2List.push(val);
                          }
                          extraElement2.children = extraElement2List; // 将btn复制给children
                      }
                      element2.children = element2List; // 将btn复制给children
                      if (element2.children && Array.isArray(element2.children)) {
                          for (let index3 = 0; index3 < element2.children.length; index3++) {
                              const element3 = element2.children[index3];
                              let extraElement3 = null;
                              if(extraElement2){
                                  extraElement3 = extraElement2.children[index3]
                              }
                              if (element3.selected == 1 || (extraElement2 && extraElement3 && extraElement3.selected == 1)) { //表示选中
                                  btnAuth.push(element2.key + '-' + element3.key + '-');
                              }
                          }
                      }
                      if (element2.selected == 1 || (extraElement2 && extraElement2.selected == 1)) {
                          pageAuth.push(element2.key);
                      }
                  }
              }
          }
      }
      return [pageAuth,btnAuth];
  }

/**
 * 是否为自己工厂id
 * @param relationship_id 
 * @returns 
 */
export function isJinshanFactory(relationship_id: number){ // TODO 金山工厂特殊标识符
   if(relationship_id == 4 || relationship_id == 25){
    return true;
   }else{
    return false;
   }
}
/**
 * 是否为纯数字
 * @param input 输入内容
 * @returns 
 */
export function isNumeric(input: any) {
    return /^\d+$/.test(input);
}

/**
 * 本地缓存配料单
 * @param pkNumber 配料id
 * @param type add 新增、remove 移除
 */
export async function updatePKList(pkNumber: string,type:string){
    let localList = await  storage.load({key: 'localPKList'}).then(pklist => pklist).catch(() => []);
    let findIndex = localList.findIndex( (item: any) => item ==  pkNumber );
    if(type == 'add'){ // 新增
        if(findIndex == -1){
            localList.push(pkNumber);
        }
    }else if(type == 'remove'){
        if(findIndex != -1){
            localList.splice( findIndex, 1 );
        }
    }
    storage.save({key: 'localPKList', data: localList});
}

/**
 * 获取所有已配料id
 * @returns 
 */
export async function getPKList(){
    return await  storage.load({key: 'localPKList'}).then(pklist => pklist).catch(() => []);
}

/**
 * 配料单是否存在
 * @param pk 配料单id
 */
export async function existsPK(pk: string){
    let localList = await  storage.load({key: 'localPKList'}).then(pklist => pklist).catch(() => []);
    let findIndex = localList.findIndex((item: any) => item == pk);
    if (findIndex != -1) {
     return true;
    }
    return false;
}